﻿using System;
using System.Drawing;
using System.IO;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;
using Label = System.Reflection.Emit.Label;
using System.Collections.Generic;
namespace HRM.UI.HRManagerUI.AddNotice
{
    public partial class PublishedNotice : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetAllNotices();
            }
        }

        private void GetAllNotices()
        {
            NoticeManager manager = new NoticeManager();
            noticesGdv.DataSource = manager.GetAllNotices();
            noticesGdv.DataBind();
        }

        protected void Published(object o, EventArgs e)
        {
            try
            {
                DAL.DAO.Notice notice = new DAL.DAO.Notice();
                notice.Date = txtDate.Value;
                notice.Subject = txtSubject.Value;
                notice.Summary = txtSummary.Value.Replace("13", "</br>");

                string noticeImage = Path.GetFileName(UploadImage.PostedFile.FileName);
                if (noticeImage != "")
                {
                    UploadImage.PostedFile.SaveAs(Server.MapPath("~/NoticeImage/") + noticeImage);
                    notice.NoticeImage = "~/NoticeImage/" + noticeImage + "-" + notice.Date;
                }
                else
                {
                    notice.NoticeImage = " ";
                }
                notice.NoticeBy = txtAuthoruty.Value;

                NoticeManager manager = new NoticeManager();
                if (manager.PublishNotice(notice))
                {
                    msgLbl.ForeColor = Color.Green;
                    msgLbl.Text = "Notice Published Successfully";
                    ClearInputs();
                    GetAllNotices();
                }
                else
                {
                    msgLbl.ForeColor = Color.Red;
                    msgLbl.Text = "Notice Not Published";
                }

            }
            catch (Exception ex)
            {
                msgLbl.ForeColor = Color.Red;
                msgLbl.Text = ex.Message;
            }
        }

        private void ClearInputs()
        {
            txtDate.Value = "";
            txtSubject.Value = "";
            txtSummary.Value = "";
            txtAuthoruty.Value = "";
        }

        protected void noticesGdv_PageIndexChanging(object sender,GridViewPageEventArgs e)
        {
            noticesGdv.PageIndex = e.NewPageIndex;
            GetAllNotices();
        }

        protected void noticesGdv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                NoticeManager noticeManager = new NoticeManager();
                Notice aNotice=new Notice();
                int id = Convert.ToInt32(((System.Web.UI.WebControls.Label)noticesGdv.Rows[e.RowIndex].FindControl("lblId")).Text);
                if (noticeManager.DeleteNotice(id))
                {
                    msgLbl.Text = "Notice Deleted Successfully";
                    msgLbl.ForeColor = Color.Green;
                
                }

                GetAllNotices();


            }
            catch (Exception ex)
            {

                msgLbl.Text = ex.Message;
            }
        }
    }
}
